/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bridge;

/**
 *
 * @author victor
 */
public class Habitant extends Thread {

    private final Bridge bridge;
    private final int codeName;
    private char direction;

    public Habitant(Bridge bridge, int codeName, char direction) {
        this.bridge = bridge;
        this.codeName = codeName;
        this.direction = direction;
    }

    @Override
    public void run() {
        try {
            this.bridge.requestTurn(this);
            Printer.println("Turn asigned to: " + toString());

            //this.bridge.take(direction);

            int timeDelay = (int) (Math.random() * 10);
            Printer.println("The habitant named " + toString() + " is crossing the bridge per " + timeDelay + " seconds");

            try {
                Thread.sleep(timeDelay * 1000);
            } catch (InterruptedException e) {
            }

            this.bridge.exit();

            Printer.println("The habitant named " + toString() + " leave the bridge");
            //this.puente.pasar(this.id);

            //this.puente.liberar();
        } catch (InterruptedException ex) {
        }
    }

    @Override
    public String toString() {
        return "H" + codeName; //To change body of generated methods, choose Tools | Templates.
    }

    public char getDirection() {
        return direction;
    }
}
